#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<sys/types.h>
#include<sys/wait.h>

int main()
{

  pid_t id =fork();
 
  //环境变量指针声明
  extern char** environ;
  
  char * const argv[]={
   (char*)"ls",
   (char*)"-a",
   (char*)"-l",
    NULL 
 };
  if(id==0)
  {

    //child
    //让子进程执行全新的程序，以前是执行父进程的代码
    printf("我是子进程，pid: %d\n",getpid());
    //char* const env_[]={
    // (char*)"MYPATH=HIHIHI",
    //   NULL
    //};

    char* env_[100]={NULL};
    for(int i=0;environ[i];i++)
    {
      env_[i]=(char*)environ[i];
      if(!environ[i+1]&&environ[i])
      {
       env_[i+1]="MYPATH=HIHIHI";
      }
    }

    for(int i=0;env_[i];i++)
    {
      printf("%s\n",env_[i]);
    }
    
    execle("./mycmd","mycmd",NULL,env_);
   // execle("./mycmd","mycmd",NULL,environ);
    // execl("/usr/bin/bash","bash","test.sh",NULL);
   // execl("./mycmd","mycmd",NULL);
    //execvp("ls",argv);
    // execlp("ls","ls","-a","-l",NULL);
    //execv("/usr/bin/ls",argv);
    //execl("/usr/bin/ls","ls","-a","-l",NULL);
    printf("失败\n");
    exit(1);//只要执行了exit,意味着，execl系列的函数失败了

  }
  //一定是父进程
  int status=0;
  printf("我是父进程，我的pid :%d\n",getpid());
  int ret = waitpid(id,&status,0);
  if(ret==id)
  {
    printf("父进程等待成功\n");
  }

  //printf("我执行完了，我的pid : %d",getpid());


  return 0;
}
